//
//  ZXStatusPhotosVIew.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXStatusPhotosVIew.h"
#import "ZXStatusPhotoView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
//一共有多少图片
#define PhotosMaxCount 9
//图片间隔
#define photoMagin 10
#define maxColumns(count) (count == 4 ? 2 : 3)
//图宽
#define photoW  75
//图高
#define photoH  photoW

@interface ZXStatusPhotosVIew()

//记录当前遮盖imgVIew的frm
@property(nonatomic,weak)UIImageView* currentCoverImgView;
//原来的位置
@property(nonatomic,assign)CGRect sourceCurrentCoverImgViewFrm;

@end

@implementation ZXStatusPhotosVIew


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
//      初始化9个UIImageView ，多余的隐藏
        for (NSInteger i = 0; i < PhotosMaxCount; i++)
        {
            ZXStatusPhotoView * photoView = [[ZXStatusPhotoView alloc]init];
            photoView.userInteractionEnabled = YES;
            photoView.backgroundColor = [UIColor lightGrayColor];
            photoView.tag = i;
            //给图片添加点击手势
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgClick:)];
            [photoView addGestureRecognizer:tap];
            
            [self addSubview:photoView];
        }
    }
    return self;
}
//点击图片的调用的操作
-(void)imgClick:(UITapGestureRecognizer *)recognizer
{
    //定义一个图片浏览器
    MJPhotoBrowser * browser = [[MJPhotoBrowser alloc]init];
    
    //设置图片数据- 图片数据必须是MJPhoto的数组
    NSMutableArray * mjphotos = [NSMutableArray array];
    
    //吧zxphotot 转成MJPhoto
    NSInteger imgCount = self.pic_urls.count;
    for (NSInteger i = 0; i < imgCount; i++)
    {
        ZXPhoto * zxphoto = self.pic_urls[i];
        
        MJPhoto * mjphoto = [[MJPhoto alloc]init];
        
        //设置大图片url
        mjphoto.url = [NSURL URLWithString:zxphoto.bmiddle_pic];
        
        //设置来源
//        mjphoto.srcImageView = (UIImageView *)recognizer.view;
        mjphoto.srcImageView = (UIImageView *)self.subviews[i];
        
        [mjphotos addObject:mjphoto];
    }
    
    browser.photos = mjphotos;
    
    //设置当前索引
    browser.currentPhotoIndex = recognizer.view.tag;
    
    //显示
    [browser show];
    
}
/**
 *  点击图片的调用的操作
 */
-(void)imgClick1:(UITapGestureRecognizer *)recognizer
{
    ZXLog(@"点击了图片");
//    添加遮盖视图
    UIView * cover = [[UIView alloc]init];
    cover.frame = [UIScreen mainScreen].bounds;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0;
    
//    再给遮盖视图添加一个点击事件
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverClick:)];
    [cover addGestureRecognizer:tap];
    [self.window addSubview:cover];
    
//    向遮盖添加一图片
    //获取到点击的照片
    UIImageView * clickImgView = (UIImageView *)recognizer.view;
    
    //创建一个遮盖的image
    UIImageView * coverImgView = [[UIImageView alloc]init];
//    coverImgView.image = clickImgView.image;
#warning 显示大图片 ，用小图片做占位
    ZXPhoto * photo = self.pic_urls[clickImgView.tag];
    NSURL *bigImgUrl = [NSURL URLWithString:photo.bmiddle_pic];
    [coverImgView sd_setImageWithURL:bigImgUrl placeholderImage:clickImgView.image];
    //吧点击图片的frm 转成 窗口的frm
    CGRect coverImgViewFrm = [clickImgView.superview convertRect:clickImgView.frame toView:self.window];
    
    self.sourceCurrentCoverImgViewFrm = coverImgViewFrm;
    coverImgView.frame = coverImgViewFrm;
    [cover addSubview:coverImgView];
     self.currentCoverImgView = coverImgView;
    
//    实现遮盖上UImgView放大的效果
    [UIView animateWithDuration:0.25 animations:^{
//  重新设置coverImgViewFrm
        CGFloat changeW = UIScreenW;
        CGFloat changeH = changeW;
        CGFloat changeX = 0;
        CGFloat changeY = (cover.size.height - changeH) * 0.5;
        coverImgView.frame = CGRectMake(changeX, changeY, changeW, changeH);
         cover.alpha = 1;
    }];
    
    
    
    
}

//遮盖视图的点击事件
-(void)coverClick:(UITapGestureRecognizer *)recognizer
{
    [UIView animateWithDuration:0.25 animations:^{
        self.currentCoverImgView.frame = self.sourceCurrentCoverImgViewFrm;
        recognizer.view.alpha = 0;
    } completion:^(BOOL finished) {
        [recognizer.view removeFromSuperview];
    }];
    
    
}

+(CGSize)photosSizeWithPicCount:(NSInteger)count
{
    
//    CGFloat photoW = 75;
//    CGFloat photoH = photoW;
    
    if (count == 0)
    {
        return CGSizeMake(0, 0);
    }
    
    
    NSInteger photosCount = count;
//    NSInteger columns = photosCount == 4 ? 2 : 3;
    NSInteger columns = maxColumns(photosCount);
    
    //2
//    NSInteger rows = (photosCount - 1) / columns + 1;
    NSInteger rows = photosCount / columns;
    if (photosCount % columns != 0)
    {
        rows ++;
    }
    CGFloat photosW = (photoW + photoMagin) * columns - photoMagin;
    CGFloat photosH = (photoH + photoMagin) * rows - photoMagin;
    
    return CGSizeMake(photosW, photosH);
}

-(void)setPic_urls:(NSArray *)pic_urls
{
    _pic_urls = pic_urls;
    
//    设置子控件的frm
    NSInteger photosCount = pic_urls.count;
//    总列数
    NSInteger columns = maxColumns(photosCount);

    
    for (NSInteger i = 0; i < PhotosMaxCount; i++)
    {
        ZXStatusPhotoView * photoView = self.subviews[i];
        if (i < photosCount)
        {
            photoView.hidden = NO;
            CGFloat column = i % columns;
            CGFloat row = i / columns;
            //子控件frm
            CGFloat photoX = (photoW + photoMagin) * column;
            CGFloat photoY = (photoH + photoMagin) * row;
            photoView.photo = pic_urls[i];
            
            photoView.frame = CGRectMake(photoX, photoY, photoW, photoH);
            
        }
        else
        {//多余的隐藏
            photoView.hidden = YES;
        }
    }
}

@end
